package com.example.yuejian.yuejian.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.example.yuejian.yuejian.R;

import java.util.ArrayList;
import java.util.List;

import static com.example.yuejian.yuejian.view.Carousel.setCarousel;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements View.OnClickListener {
    private RecyclerView recyclerView;
    //private RecyclerViewHeader header;
    private View view=null;
    //private UserListAdapter mAdapter;
    // private  List<String> mDatas=new ArrayList<>();

    private LinearLayout mTabActive;
    private LinearLayout mTabTeacher;
    private LinearLayout mTabMatch;
    private LinearLayout mTabAll;

    private TextView tvActive;
    private TextView tvTeacher;
    private TextView tvMatch;

    private Fragment mFmActive;
    private Fragment mFmTeacher;
    private Fragment mFmMatch;



    private Context context;
    private ConvenientBanner convenient;//顶部广告栏控件
    private LinearLayout li;
    private ViewGroup.LayoutParams i;
    private String[] image = {"company1.jpg","company2.jpg","company3.jpg","company4.jpg"};
    private List<String> images =new ArrayList<>();
    // private List<String> imgUrl =new ArrayList<>();
    // String imgUrl;
    String imgUrl="http://123.207.174.155/zqb/Public/goodsImg/";

    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       view= inflater.inflate(R.layout.fragment_home, container, false);
        initView();
        initEvents();
        setSelect(1);
        return view;
    }
    private void initView() {
        mTabActive=(LinearLayout)view.findViewById(R.id.id_tab_home_active);
        mTabTeacher=(LinearLayout)view.findViewById(R.id.id_tab_home_teacher);
        mTabMatch=(LinearLayout)view.findViewById(R.id.id_tab_home_match);

        mTabAll=(LinearLayout)view.findViewById(R.id.id_tab_home_top_all);

        tvActive=(TextView)view.findViewById(R.id.tv_tab_home_active);
        tvTeacher=(TextView)view.findViewById(R.id.tv_tab_home_teacher);
        tvMatch=(TextView)view.findViewById(R.id.tv_tab_home_match);



        for (int i = 0; i < 4; i++) {
            images.add(imgUrl + image[i]);
        }
        context = getActivity();
        li = (LinearLayout) view.findViewById(R.id.play);
        // header = (RecyclerViewHeader)view.findViewById(R.id.recycle_header);
        convenient = (ConvenientBanner) view.findViewById(R.id.convenientBanner);
        Display d = getActivity().getWindowManager().getDefaultDisplay();
        //*获取布局参数*//*
        i = li.getLayoutParams();
        i.height = d.getHeight() / 3;
        i.width = d.getWidth();
        setCarousel(context, convenient, images, i.width, i.height);
       /* CarouselModel carouselModel=new  CarouselModel();
        Call<List<Carousel>> call1=carouselModel.Carousel();
        call1.enqueue(new Callback<List<Carousel>>() {
            @Override
            public void onResponse(Call<List<Carousel>> call1, retrofit2.Response<List<Carousel>> response)
            {
                List<Carousel> res=response.body();
                for (int i=0;i<res.size();i++)
                {
                    images.add(res.get(i).getPhoto());
                }
                context=getActivity();
                li=(LinearLayout)view.findViewById(R.id.play);
                // header = (RecyclerViewHeader)view.findViewById(R.id.recycle_header);
                convenient = (ConvenientBanner)view.findViewById(R.id.convenientBanner);
                Display d=getActivity().getWindowManager().getDefaultDisplay();
               *//*获取布局参数*//*
                i=li.getLayoutParams();
                i.height=d.getHeight()/3;
                i.width=d.getWidth();
                setCarousel(context,convenient,images,i.width,i.height);

            }
            @Override
            public void onFailure(Call<List<Carousel>> call, Throwable t) {

            }
        });

    }*/
    }
    private void initEvents(){
        mTabActive.setOnClickListener(this);
        mTabTeacher.setOnClickListener(this);
        mTabMatch.setOnClickListener(this);
    }

    private void resetTextColors() {

        tvActive.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray));
        tvTeacher.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray));
        tvMatch.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray));
    }
    private  void resetBgColors(){
        mTabActive.setBackgroundResource(R.drawable.shape_home_top);
        mTabTeacher.setBackgroundResource(R.drawable.shape_home_top);
        mTabMatch.setBackgroundResource(R.drawable.shape_home_top);
        //mTabUserphoto.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));


    }

    @Override
    public void onClick(View v) {

        resetTextColors();
        resetBgColors();
        switch (v.getId())
        {
            case R.id.id_tab_home_active:
                setSelect(0);
                break;
            case R.id.id_tab_home_teacher:
                setSelect(1);
                break;
            case R.id.id_tab_home_match:
                setSelect(2);
                break;
            default:
                break;
        }
    }
    private void setSelect(int i) {
        FragmentManager fm =getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mFmActive == null)
                {
                    mFmActive = new HomeActiveFragment();
                    transaction.add(R.id.id_content_home, mFmActive);
                } else
                {
                    transaction.show(mFmActive);
                }
                tvActive.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                mTabActive.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.gray));

                break;
            case 1:
                if (mFmTeacher == null)
                {
                    mFmTeacher = new HomeTeacherFragment();
                    transaction.add(R.id.id_content_home, mFmTeacher);
                } else
                {
                    transaction.show(mFmTeacher);

                }

                tvTeacher.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                mTabTeacher.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.gray));

                break;
            case 2:
                if (mFmMatch == null)
                {
                    mFmMatch = new HomeMatchFragment();
                    transaction.add(R.id.id_content_home, mFmMatch);
                } else
                {
                    transaction.show(mFmMatch);

                }

                tvMatch.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
                mTabMatch.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.gray));

                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mFmActive != null)
        {
            transaction.hide(mFmActive);
        }
        if (mFmTeacher != null)
        {
            transaction.hide(mFmTeacher);
        }
        if (mFmMatch != null)
        {
            transaction.hide(mFmMatch);
        }

    }
}
